package org.ss.psci.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.ss.psci.entity.PmCompany;
import org.ss.psci.entity.PmProjects;
import org.ss.psci.entity.PmRight;
import org.ss.psci.entity.PmRole;
import org.ss.psci.entity.PmUser;
import org.ss.psci.util.Encrypt;
import org.ss.psci.util.ListRange;

public class UserService {

	private BaseDAO baseDAO;
	private List list;
	
	public PmCompany getCompanyinfo(String companyId) {
		PmCompany pc = new PmCompany();
		List uList = baseDAO.getPmCompanyDAO().findByCompanyId(companyId);
		if (uList != null) {
			if (uList.size() != 0) {
				pc = (PmCompany) uList.get(0);
			}
		}
		return pc;
	}
	
	public void saveUserinfo(Map formMap) {
		PmUser pu = new PmUser();

		try {
			BeanUtils.populate(pu, formMap);
			PmUser user = getUserinfo(pu.getUsername());
			if (user.getPassword().equals(Encrypt.crypt(pu.getOldpass()))) {
				user.setPassword(Encrypt.crypt(pu.getConfirmpass()));
			}
			user.setAddress(pu.getAddress());
			user.setCompany(pu.getCompany());
			user.setPhone(pu.getPhone());
			user.setRealname(pu.getRealname());
			user.setNickname(pu.getRealname());
			baseDAO.getPmUserDAO().save(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void saveNewUserinfo(String username, Map formMap) {
		PmUser pu = new PmUser();
		try {
			BeanUtils.populate(pu, formMap);
			PmUser user = getUserinfo(username);
			if (user != null) {
				String p=pu.getPassword();
				pu.setPassword(Encrypt.crypt(p));
			
				user=baseDAO.getPmUserDAO().copyEntity(user, pu);				
				
				baseDAO.getPmUserDAO().save(user);
			} else {
				baseDAO.getPmUserDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delUserinfo(String username) {
		try {
			PmUser user = getUserinfo(username);
			if (user != null) {
				baseDAO.getPmUserDAO().delete(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmUser getUserinfo(String username) {
		PmUser pu = new PmUser();
		List uList = baseDAO.getPmUserDAO().findByUsername(username);
		if (uList != null) {
			if (uList.size() != 0) {
				pu = (PmUser) uList.get(0);
			}
		}
		return pu;
	}

	public String checkUser(String username, String password,
			HttpServletRequest request) {
		if(username==null || password==null){
			return "";
		}
		if("".equals(username) ||"".equals(password)){
			return "";
		}
		List uList = baseDAO.getPmUserDAO().findByUsernameAndPassword(username,
				Encrypt.crypt(password));
		if (uList != null) {
			if (uList.size() != 0) {
				PmUser pu = (PmUser) uList.get(0);
				return pu.getUsername();
			}
		}
		return "";
	}

	public ListRange getUsers() {
		ListRange range = new ListRange();
		List uList = baseDAO.getPmUserDAO().findAll();
		range.setData(uList.toArray());
		range.setTotalSize(uList.size());
		System.out.println("uList.size()=" + uList.size());
		return range;
	}

	public ListRange getItems(Map condition) {

		int rows, start, limit;

		try {

			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());

				System.out.println("start=" + start + ";limit=" + limit
						+ ";sort=" + condition.get("sort") + ";dir="
						+ condition.get("dir"));
				System.out.println("param1=" + condition.get("param1")
						+ ";param2=" + condition.get("param2"));

			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}

			if ((list == null) || (start == 0)) {
				list = baseDAO.getPmUserDAO().findAll();
			}

			if (list == null) {
				ListRange listRange = new ListRange();
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}

			List result = new ArrayList();
			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}

			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public ListRange getRights(Map condition) {
		int rows, start, limit;
		try {
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				list = baseDAO.getPmRightDAO().findAll();
			}
			if (list == null) {
				ListRange listRange = new ListRange();
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}
			List result = new ArrayList();
			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void saveRight(String temp, Map formMap) {
		PmRight pu = new PmRight();
		try {
			BeanUtils.populate(pu, formMap);
			PmRight p = getRight(temp);
			if (p != null) {
				p=baseDAO.getPmRightDAO().copyEntity(p, pu);				
				
				baseDAO.getPmRightDAO().save(p);
			} else {
				baseDAO.getPmRightDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delRight(String temp) {
		try {
			PmRight p = getRight(temp);
			if (p != null) {
				baseDAO.getPmRightDAO().delete(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmRight getRight(String temp) {
		PmRight p = new PmRight();
		List uList = baseDAO.getPmRightDAO().findByRightName(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				p = (PmRight) uList.get(0);
			}
		}
		return p;
	}

	public ListRange getRoles(Map condition) {
		int rows, start, limit;
		try {
			try {
				start = Integer.parseInt(condition.get("start").toString());
				limit = Integer.parseInt(condition.get("limit").toString());
			} catch (Exception ex) {
				start = 0;
				limit = 10;
			}
			if ((list == null) || (start == 0)) {
				list = baseDAO.getPmRoleDAO().findAll();
			}
			if (list == null) {
				ListRange listRange = new ListRange();
				return listRange;
			} else {
				rows = start + limit > list.size() ? list.size() : start
						+ limit;
			}
			List result = new ArrayList();
			for (int i = start; i < rows; i++) {
				result.add(list.get(i));
			}
			ListRange listRange = new ListRange();
			listRange.setData(result.toArray());
			listRange.setTotalSize(list.size());

			return listRange;
		} catch (Exception ex) {
			System.out.println(ex.toString());
			ListRange listRange = new ListRange();
			return listRange;
		}
	}

	public void saveRole(String temp, Map formMap) {
		PmRole pu = new PmRole();
		try {
			BeanUtils.populate(pu, formMap);
			PmRole p = getRole(temp);
			if (p != null) {
				p=baseDAO.getPmRoleDAO().copyEntity(p, pu);				
				
				baseDAO.getPmRoleDAO().save(p);
			} else {
				baseDAO.getPmRoleDAO().save(pu);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void delRole(String temp) {
		try {
			PmRole p = getRole(temp);
			if (p != null) {
				baseDAO.getPmRoleDAO().delete(p);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PmRole getRole(String temp) {
		PmRole p = new PmRole();
		List uList = baseDAO.getPmRoleDAO().findByRoleName(temp);
		if (uList != null) {
			if (uList.size() != 0) {
				p = (PmRole) uList.get(0);
			}
		}
		return p;
	}

	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

}
